﻿using System;
using System.IO;
using System.Linq;
using Microsoft.Web.Administration;

namespace LINQPadLogParser
{
  public class LogParserDataContext
  {
    internal static MultipleLineManager LineManager { get; set; }

    public ISiteLog Site(int id)
    {
      var manager = new ServerManager();
      Site site = manager.Sites.Single(a => a.Id == id);
      Binding binding = site.Bindings[0];
      string host = binding.Host;
      if (string.IsNullOrWhiteSpace(host))
        host = binding.EndPoint.Address.ToString();
      string port = binding.EndPoint.Port == 80 ? string.Empty : ":" + binding.EndPoint.Port;
      var uri = new Uri(string.Format("{0}://{1}{2}", binding.Protocol, host, port));
      return
        new W3CLogEnumerable(
          Path.Combine(site.LogFile.Directory.Replace("%SystemDrive%", Environment.GetEnvironmentVariable("SystemDrive")), String.Format(@"W3SVC{0}\", id)), uri);
    }
  }
}